package org.Image.management.SpringDataService.Service.Controller;

import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.Image.management.SpringDataService.Pojo.Result.Result;
import org.Image.management.SpringDataService.Service.StorageIo.Pojo.Mapper.FileStorage;
import org.Image.management.SpringDataService.Service.StorageIo.Pojo.Vo.UploadFileVo;
import org.Image.management.SpringDataService.Service.StorageIo.service.FileService;
import org.Image.management.utils.JsonFileUtils;
import org.springframework.stereotype.Controller;

import java.io.File;
import java.util.List;

@Controller
@RequiredArgsConstructor
@Slf4j
public class StorageController {
    private final FileService fileService;

    //删除文件
    public Result<?> deleteFile(@NotBlank(message = "请选择进行删除") String file, Long userId) {
        //删除数据库中的记录
        if (!fileService.deleteFile(file, userId)) {
            return Result.failure("文件不存在");
        }

        //删除文件
        return Result.success("文件删除成功");
    }

    //上传文件
    public Result<?> uploadFile(File file, Long id, String description) {

        try {
            UploadFileVo uploadFileVo = fileService.uploadFile(file, id, description);
            JsonFileUtils.saveJsonToFile("上传文件", uploadFileVo);

            return Result.success(uploadFileVo);

        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.getMessage());
        }
    }

    // 获取用户图片
    public Result<?> getPicture(Long user) {

        try {
            List<FileStorage> fileStorages = fileService.selectByUserId(user);
            return Result.success(fileStorages);

        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.getMessage());
        }
    }

    //
    public Result<?> inquireString(Long user, String inquireString) {
        try {
            List<FileStorage> fileStorages = fileService.selectByInquireString(user, inquireString);
            return Result.success(fileStorages);

        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure(e.getMessage());
        }
    }

    public void reviseRemark(String uuid, Long userid, String remark) {
        log.info("修改备注信息 uuid：{},userid: {},remark： {}", uuid, userid, remark);
        fileService.updateFile(FileStorage.builder().uuid(uuid).userId(userid).description(remark).build());
    }

    public void reviseImage(Long userid,String uuid, File customData) {
        fileService.reviseImage(userid,uuid, customData);
    }
}
